using System;

namespace RockHopper.Common.Model
{
    public static class UnitConverter
    {
        public static double ToMetres(this double value, UnitType units)
        {
            if (units == UnitType.Metres) return value;

            if (units  == UnitType.Kilometres) return 1000*value;

            return 1609.344*value;
        }

        public static double ToMinutesPer(this double value, PaceUnitType units)
        {
            if (units == PaceUnitType.Kilometres) return value;

            return 1.609344*value;
        }

        public static UnitType ToUnitType(this string value)
        {
            if (!value.EndsWith("s"))
                value += "s";
            return (UnitType) Enum.Parse(typeof (UnitType), value, true);
        }

        public static string FromUnitType(this UnitType units, double value)
        {
            if (Math.Abs(value - 1) < 0.0000001)
            {
                return units.ToString().TrimEnd('s');
            }

            return units.ToString();
        }
    }
}